package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Staff;
import com.example.service.StaffService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/staffs")
@Api(tags = "员工相关接口")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    @ApiOperation(value = "查询接口")
    public Result selectStaff() {
        List<Staff> staff = staffService.selectStaff();
        Integer code = staff != null ? Code.GET_OK : Code.GET_ERR;
        String msg = staff != null ? "" : "数据查询失败，请重试！";
        return new Result(code, staff, msg);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "单个查询接口")
    public Result selectStaffById(@PathVariable Integer id) {
        Staff staffs = null;

        String key = "Staff_ID：" +id;
        //先从redis中获取缓存数据
        staffs = (Staff) redisTemplate.opsForValue().get(key);

        if(staffs != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,staffs,"查询成功！");
        }
        staffs = staffService.selectStaffById(id);
        redisTemplate.opsForValue().set(key,staffs,60, TimeUnit.MINUTES);

        String msg = staffs!=null ? "" : "数据查询失败，请重试！";
        Integer code = staffs != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,staffs,msg);
    }

    @PostMapping("/ByName")
    @ApiOperation(value = "根据名字查询接口")
    public Result selectStaffByName(@RequestBody Staff staff){
        List<Staff> staff1 = staffService.selectStaffByName(staff);
        String msg = staff1!=null ? "" : "数据查询失败，请重试！";
        Integer code = staff1 != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,staff1,msg);
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加员工接口")
    public Result insertStaff(@RequestBody Staff staff) {
        int i = staffService.addStaff(staff);
        String msg = i > 0 ? "" : "数据添加失败，请重试！";
        if (i > 0) {
            System.out.println("插入员工成功");
            return new Result(Code.INSERT_OK, i, msg);
        } else {
            System.out.println("插入失败");
            return new Result(Code.INSERT_ERR, i, msg);
        }

    }

    @PutMapping
    @ApiOperation(value = "更新员工接口")
    public Result updateStaff(@RequestBody Staff staff) {
        int i = staffService.updateStaff(staff);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";

        //删除redis中缓存
        String key = "Staff_ID：" + staff.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功！");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败！");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工接口")
    public Result deleteStaff(@PathVariable Integer id) {
        int i = staffService.deleteStaff(id);
        String msg = i > 0 ? "" : "数据删除失败，请重试！";

        //删除redis中缓存
        String key = "Staff_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功！");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败！");
            return new Result(Code.DELETE_ERR, i, msg);
        }
    }
    @GetMapping("/{page}/{size}")
    @ApiOperation(value = "分页查询接口")
    public Result findUserList(@PathVariable int page,@PathVariable int size) {
        PageInfo<Staff> staffPage = staffService.findPage(page, size);
        Integer code = staffPage != null ? Code.GET_OK : Code.GET_ERR;
        String msg = staffPage != null ? "" : "分页失败，请重试！";
        return new Result(code, staffPage, msg);
    }
    @RequestMapping("/makeExcel")
    @ApiOperation(value = "Excel表格导出接口")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "staff.xlsx", staffService.getOutputExcelProjectData());
    }
}
